package com.peixunp.dao.impl;

import java.util.List;
import java.util.Map;

import com.peixunp.bean.Information;
import com.peixunp.dao.InformationDao;
import com.peixunp.util.Constants;

import finaler.db.AbstractBaseDao;
import finaler.util.Page;
import finaler.util.PageList;
import finaler.util.SQLUtil;

public class InformationDaoImpl extends AbstractBaseDao<Information> implements InformationDao {

	@Override
	protected Class<Information> getEntityClass() {
		return Information.class;
	}

	@Override
	protected boolean isDebug() {
		return Constants.ISDEBUG;
	}

	@Override
	public List<Information> getInformationList(int orgid) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append(" select ifcontent,createtime from information ");
		sql.append(" where orgid = "+orgid);
		sql.append(" and auditflag = 1");
		sql.append(" order by id desc");
		return super.findBySqlToBeanList(sql.toString());
	}

	@Override
	public List<Information> getAuditList() throws Exception {
		String sql  = "select * from Information where auditflag = 0";
		return super.findBySqlToBeanList(sql);
	}

	@Override
	public PageList getAllInfoList(Page page) throws Exception {

		StringBuffer whereSql = new StringBuffer();
		whereSql.append(" where a.auditflag = 1");
		if(page.getParameter("infoKey")!=null && page.getParameter("infoKey").toString().equals("")==false){
			whereSql.append(" and  ifcontent like '%" + page.getParameter("infoKey") +"%'");
		}
		PageList pageList = new PageList(page);//固定
		StringBuffer sqlCount = new StringBuffer();
		sqlCount.append("select count(1) from Information a");
		sqlCount.append(" inner join organizations  b on a.orgid = b.id ");
		sqlCount.append( whereSql );
		int dataCount = super.getIntBySql( sqlCount.toString() );//取总
		pageList.setTotal(dataCount);//总数
		
		StringBuffer queryListSql = new StringBuffer();
		queryListSql.append(" select a.*,b.city,b.orgname ");
		queryListSql.append(" from Information a ");
		queryListSql.append(" inner join organizations  b on a.orgid = b.id ");
		queryListSql.append( whereSql );
		queryListSql.append(" order by id desc");
		SQLUtil pageSQL = new SQLUtil(queryListSql);//固定
		pageSQL.setFirstResult( page.getStart() );//固定
		pageSQL.setMaxResults( page.getRange() );//固定
		List<Map<String, Object>> dataList = super.findBySqlToMapList(pageSQL.getSql());//数据集
		pageList.setList(dataList);
		return pageList;
	}
	
}
